package cl.utalca.robot.service.impl;

import java.sql.Connection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cl.utalca.robot.DB.DataSourceManager;
import cl.utalca.robot.domain.LanguageDTO;
import cl.utalca.robot.persistence.LanguageDAO;
import cl.utalca.robot.service.LanguageService;

@Service
public class LanguageImpl implements LanguageService {

	@Autowired
	LanguageDAO languageDAO;
	
	@Override
	public List<LanguageDTO> getLanguages() {
		
		Connection conn = DataSourceManager.getConnection();
		
		//List<LanguageDTO> resp = languageLogic.getLanguages(c);
		List<LanguageDTO> resp = null;
		//LanguageInterface languageDB = new LanguageDAO(c);
		
		
		resp = languageDAO.getLanguages(conn);
		
		Boolean success = resp.size() > 0;
		DataSourceManager.close(conn, success);
		
		return resp;
	}

	@Override
	public LanguageDTO getLanguage(int languageId) {
		
		Connection conn = DataSourceManager.getConnection();
		LanguageDTO resp = null;
		
		resp = languageDAO.getLanguage(languageId, conn);
		
		Boolean success = resp != null;
		DataSourceManager.close(conn, success);
		
		return resp;
	}

	@Override
	public Boolean saveLanguage(LanguageDTO language) {
		
		Connection conn = DataSourceManager.getConnection();
		
		int resp = languageDAO.saveLanguage(language, conn);
		
		Boolean success = resp != 0;
		DataSourceManager.close(conn, success);
		
		return success;
	}

	@Override
	public Boolean updateLanguage(LanguageDTO language) {

		Connection c = DataSourceManager.getConnection();
		
		int resp = -1;
		resp = languageDAO.updateLanguage(language, c);
		
		Boolean success = resp > 0;
		DataSourceManager.close(c, success);
		
		return success;
	}

	@Override
	public Boolean deleteLanguage(int languageId) {
		// TODO Auto-generated method stub
		return null;
	}

}
